package servlet.soft;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.util.NewBeanInstanceStrategy;
import dao.SoftDao;
import dbutil.DBHelper;
import entity.Download;
import org.apache.commons.lang.StringUtils;

public class EditDownloadServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8073988699331063452L;

	public EditDownloadServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("applicatoin/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		String softName = request.getParameter("appName");
		String picUrl = request.getParameter("picUrl");
		String language = request.getParameter("language");
		String inner32 = request.getParameter("inner32");
		String inner64 = request.getParameter("inner64");
		String outer32 = request.getParameter("outer32");
		String outer64 = request.getParameter("outer64");
		String webDesc = request.getParameter("desc");
		
		JSONObject jsonObject = new JSONObject();
		Download download = null;
		Connection conn = null;
		SoftDao softDao = null;
		
		try {
			conn = DBHelper.getConnection();
			conn.setAutoCommit(false);
			
			softDao = new SoftDao();
			download = new Download();
			download.setAppName(softName);
			download.setAppPicURL(picUrl);
			download.setAppLanguage(language);
			download.setInner32(inner32);
			download.setInner64(inner64);
			download.setOuter32(outer32);
			download.setOuter64(outer64);
			download.setWebDesc(webDesc);
			
			if(softDao.editDownload(download)) {
				jsonObject.put("retCode", "1");
				conn.commit();
			} else {//修改失败
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "EditDownloadServlet dao");
				conn.rollback();
			}
			
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "EditDownloadServlet Exception");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}
	}

	public void init() throws ServletException {
	}

}
